package auctions;

import jade.core.AID;
import jade.core.Agent;

import jade.domain.FIPANames;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.util.Random;

/** Class that implements a buyer agent that can be configured to paricipate in
 * either an english or a dutch auction.
 */
public class BuyerAgent extends Agent {
    //the maximum price that the agent will pay
    private int budget;
    //the number of items
    private int numOfItems;
    
    private static Random rand = new Random();
    
    protected void setup() {
        //get the arguments for the agent
        Object[] arguments = getArguments();
        if(arguments.length < 1) {
            System.err.println("Usage agentName [english price | " +
                "dutch price flowers");
            doDelete();
        } else {
            //get the price and utility
            try {
                budget = Integer.parseInt(arguments[1].toString());
            } catch (NumberFormatException e) {
                budget = 100 + rand.nextInt(100);
            } catch (NullPointerException e) {
                budget = 100 + rand.nextInt(100);
            }
            if(arguments[0].toString().equals(SellerAgent.EN)) {
                //set the number of items to one
                numOfItems = 1;
                //add the behaviour to buy the item
                addEnglishBuyerBehaviour();
            } else {
                try {
                    numOfItems = Integer.parseInt(arguments[2].toString());
                } catch (NumberFormatException e) {
                    numOfItems = 1 + rand.nextInt(3);
                } catch (NullPointerException e) {
                    numOfItems = 1 + rand.nextInt(3);
                }
                //add the behaviour to buy the flowers
                addDutchBuyerBehaviour();
            }
        }
    }

    private void addEnglishBuyerBehaviour() {
        MessageTemplate mt = MessageTemplate.and(
            MessageTemplate.MatchProtocol(
                FIPANames.InteractionProtocol.FIPA_ENGLISH_AUCTION),
            MessageTemplate.or( 
                MessageTemplate.MatchPerformative(ACLMessage.REQUEST),
                MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL)));
        EnglishSimpleBuyer s = new EnglishSimpleBuyer();
        addBehaviour(new EnglishBuyer(this, mt, s, budget));
    }

    private void addDutchBuyerBehaviour() {
        MessageTemplate mt = MessageTemplate.and(
            MessageTemplate.MatchProtocol(
                FIPANames.InteractionProtocol.FIPA_DUTCH_AUCTION),
            MessageTemplate.MatchPerformative(ACLMessage.REQUEST));
        DutchSimpleBuyer s = new DutchSimpleBuyer();
        addBehaviour(new DutchBuyer(this, mt, s, budget, numOfItems));
    }
    
    void sendBuyerInfo(AID destination, int budget, int items) {
        //used to send information about this buyer to the user interface 
        //manager agent
        ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
        msg.setContent(SellerAgent.buildContent(budget, items));
        msg.addReceiver(destination);
        send(msg);
    }
}
